package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.config.ben.ServerResponseEntity;
import com.example.demo.entity.AnIndexImg;
import com.example.demo.entity.DTO.IndexImgDto;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IAnIndexImgService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页轮播图的表 前端控制器
 * </p>
 *
 * @author Mr.we
 * @since 2024-06-03
 */
@Api(tags = "首页轮播图的接口")
@RestController
@RequestMapping("/an-index-img")
public class AnIndexImgController {

    @Autowired
    private IAnIndexImgService anIndexImgService;

    /**
     * 查询所有接口
     * @return 查询成功
     */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "查询首页轮播图的表所有数据")
    @GetMapping
    public Result<List<AnIndexImg>> findAll() {
        return Result.success(anIndexImgService.list()).msg("查询成功");
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @GetMapping("/{id}")
    public Result<AnIndexImg> findOne(@PathVariable Integer id) {
        return Result.success(anIndexImgService.getById(id)).msg("查询成功");
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",example="1",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页多少条",example="10",required = true)
    })
    @GetMapping("/page")
    public Result<MyPage<AnIndexImg>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<AnIndexImg> queryWrapper = new QueryWrapper<>();
        return Result.success().data(anIndexImgService.page(new Page<>(pageNum, pageSize), queryWrapper)).msg("查询成功");
    }

    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    //TODO 记得来创建这个VO
    public Result<String> add() {
        //TODO VO转Entity
        //anIndexImgService.saveOrUpdate(anIndexImg);
        return Result.success().msg("添加成功");
    }

    /**
     * 更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "更新接口")
    @PutMapping
    public Result<String> save(@RequestBody AnIndexImg anIndexImg) {
        anIndexImgService.saveOrUpdate(anIndexImg);
        return Result.success().msg("更新成功");
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        anIndexImgService.removeById(id);
        return Result.success().msg("删除成功");
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "批量删除接口")
    @PostMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        anIndexImgService.removeByIds(ids);
        return Result.success().msg("批量删除成功");
    }

    /**
     * 首页轮播图接口
     */
    @GetMapping("/indexImgs")
    @ApiOperation(value = "首页轮播图")
    public ServerResponseEntity<List<IndexImgDto>> indexImgs() {
        List<AnIndexImg> indexImgList = anIndexImgService.listIndexImg();
        List<IndexImgDto> indexImgDtos = BeanUtil.copyToList(indexImgList, IndexImgDto.class);
        return ServerResponseEntity.success(indexImgDtos);
    }
}